#!/bin/sh
# Copyright (c) Huawei Technologies Co., Ltd. 2012-2023. All rights reserved.
# Description: get oracle rac info return info.
# Arguments: SCRIPT_ROOT_PATH=$1 PID=$2
# Returns: 0 is success, other is fail, result_file return rac info
#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
set +x

SCRIPT_ROOT_PATH=$1
PID=$2
PARAM_NUM=$3

. "${SCRIPT_ROOT_PATH}"/oracle_sbin_func.sh

TMP_FILE="${STMP_PATH}/oracle_rac_info_tmp_${PID}.txt"

PARAM_CONTENT=`read_input_param "${PARAM_NUM}"`


ORA_DB_USER=`get_value "${PARAM_CONTENT}" OracleInstallUser`
ORA_GRID_USER=`get_value "${PARAM_CONTENT}" GridInstallUser`
ORACLE_BASE=`get_value "${PARAM_CONTENT}" AccessOracleBase`
ORACLE_HOME=`get_value "${PARAM_CONTENT}" AccessOracleHome`

#get oracle user info
get_oracle_user
if [ -z "${ORA_DB_USER}" ] || [ -z "${ORA_DB_GROUP}" ]; then
    log_error "Get oracle user failed"
    exit "${RETURN_INVALID}"
fi
get_oracle_user_shell_type

env_file_list=`get_user_env_file "${ORA_DB_USER}"`
if [ $? -ne 0 ] || [ -z "$env_file_list" ]; then
    log_error "Get user env file failed"
    exit "$RETURN_INVALID"
fi

EXPORT_ORACLE_ENV=`generate_export_cmd "$env_file_list" "oracle"`
if [ $? -ne 0 ] || [ -z "$EXPORT_ORACLE_ENV" ]; then
    log_error "Get user env cmd failed"
    exit "$RETURN_INVALID"
fi

# check oracle install
check_oracle_is_install

# check oracle install type
check_oracle_install_type
if [ "${DBISCLUSTER}" -ne 1 ]; then
    log_warn "Oracle is not a rac, can not get info"
    exit 
fi

#get oracle version
get_oracle_version

# get grid user
get_grid_user
# get grid user shell type
get_grid_user_shell_type
# get grid home path
get_grid_home_path

if [ -z "${ORA_GRID_USER}" ] || [ -z "${ORA_GRID_GROUP}" ] ; then
    log_error "Get grid user failed"
    exit "${RETURN_INVALID}"
fi

env_file_list=`get_user_env_file "${ORA_GRID_USER}"`
if [ $? -ne 0 ] || [ -z "$env_file_list" ]; then
    log_error "Get user env file failed"
    exit "$RETURN_INVALID"
fi

EXPORT_GRID_ENV=`generate_export_cmd "$env_file_list" "grid"`
if [ $? -ne 0 ] || [ -z "$EXPORT_GRID_ENV" ]; then
    log_error "Get user env cmd failed"
    exit "$RETURN_INVALID"
fi

# get cluster name
su - "${ORA_GRID_USER}" "${GRID_SHELL_MODE}" -c "echo \"${ORACLE_MARK}\"; ${EXPORT_GRID_ENV} ${IN_GRID_HOME}/bin/cemutlo -n" > "${TMP_FILE}" 2>&1
if [ $? -ne 0 ]; then
    log_error "Get cluster name failed, error is `cat "${TMP_FILE}"`"
    delete_file "${TMP_FILE}"
    exit "${RETURN_INVALID}"
fi
delete_oracle_mark "${TMP_FILE}"
cluster_name=`cat "${TMP_FILE}"`

# get cluster scan_ip
su - "${ORA_GRID_USER}" "${GRID_SHELL_MODE}" -c "echo \"${ORACLE_MARK}\"; ${EXPORT_GRID_ENV} srvctl config scan" > "${TMP_FILE}" 2>&1
if [ $? -ne 0 ]; then
    log_error "Get scan_config failed, error is `cat "${TMP_FILE}"`"
    delete_file "${TMP_FILE}"
    exit "${RETURN_INVALID}"
fi
delete_oracle_mark "${TMP_FILE}"

scan_config=`cat "${TMP_FILE}"`
if [ -z "$scan_config" ]; then
    delete_file "${TMP_FILE}"
    log_error "Get scan_config failed, config is none"
    exit "${RETURN_INVALID}"
fi

if [ `rd_substr ${ORA_VERSION} 1 2` -gt 11 ]; then
    scan_ip_name=`echo "$scan_config"| grep "SCAN name:" | $MYAWK -F "," '{print $1}'| $MYAWK -F ": " '{print $2}'`

    scan_ip=`echo "$scan_config"| grep "VIP:" | $MYAWK -F ": " '{print $2}'`
else
    scan_ip_name=`echo "$scan_config" | grep "SCAN VIP" |$MYAWK -F "IP: /" '{print $2}' | $MYAWK -F "/" '{print $1}'`

    scan_ip=`echo "$scan_config" | grep "SCAN VIP" |$MYAWK -F "IP: /" '{print $2}' | $MYAWK -F "/" '{print $2}'`
fi
log_info "scan_ip_name=$scan_ip_name, scan_ip=$scan_ip"

# get cluster node info
su - "${ORA_GRID_USER}" "${GRID_SHELL_MODE}" -c "echo \"${ORACLE_MARK}\"; ${EXPORT_GRID_ENV} ${IN_GRID_HOME}/bin/crsctl status server" > "${TMP_FILE}" 2>&1

if [ $? -ne 0 ]; then
    log_error "Get cluster host info failed, error is `cat "${TMP_FILE}"`"
    delete_file "${TMP_FILE}"
    exit "${RETURN_INVALID}"
fi
delete_oracle_mark "${TMP_FILE}"

cluster_host_info=`cat "${TMP_FILE}" | grep NAME | $MYAWK -F = '{print $2}'`
if [ -z "${cluster_host_info}" ]; then
    log_error "Get cluster host info failed is none"
    delete_file "${TMP_FILE}"
    exit "${RETURN_INVALID}"
fi

# return rac info to result file
echo "ClusterType;${DBISCLUSTER}" > "${RESULT_FILE}"
echo "ClusterName;${cluster_name}" >> "${RESULT_FILE}"
echo "ClusterIP;${scan_ip}" >> "${RESULT_FILE}"

for line in `echo "${cluster_host_info}"`; do
    echo "DomainName;${line}" >> "${RESULT_FILE}"
    log_info "Cluster domain name is ${line}."
done

if [ -f "${RESULT_FILE}" ]; then
    chmod 640 "${RESULT_FILE}"
fi
log_info "Get oracle rac info succ."

